package strutil

import "strings"

// StringReverse reverses input string and return.
func StringReverse(str string) string {
	runes := []rune(str)
	length := len(runes)
	for i := 0; i < length/2; i++ {
		runes[i], runes[length-i-1] = runes[length-i-1], runes[i]
	}
	return string(runes)
}

// Join concatenates the elements of its first argument to create a single string.
// The separator string sep is placed between elements in the resulting string.
func Join(elems []string, sep string) string {
	return strings.Join(elems, sep)
}

// JoinWithWrapped concatenates the elements of its first argument to create a single string.
// The separator string sep is placed between wrapped elements in the resulting string.
// The wrapper string lwrap is placed before each element, and rwrap after each element.
func JoinWithWrapped(elems []string, sep, lwrap, rwrap string) string {
	length := len(elems)
	if length == 0 {
		return ""
	}

	n := len(sep) * (length - 1)
	n += (len(lwrap) + len(rwrap)) * length
	for i := 0; i < length; i++ {
		n += len(elems[i])
	}

	var b strings.Builder
	b.Grow(n)
	b.WriteString(lwrap)
	b.WriteString(elems[0])
	b.WriteString(rwrap)
	for _, s := range elems[1:] {
		b.WriteString(sep)
		b.WriteString(lwrap)
		b.WriteString(s)
		b.WriteString(rwrap)
	}
	return b.String()

}
